/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.suli.rollDetail.service.impl;

import me.zhengjie.suli.rollDetail.domain.RollDetail;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.rollDetail.repository.RollDetailRepository;
import me.zhengjie.suli.rollDetail.service.RollDetailService;
import me.zhengjie.suli.rollDetail.service.dto.RollDetailDto;
import me.zhengjie.suli.rollDetail.service.dto.RollDetailQueryCriteria;
import me.zhengjie.suli.rollDetail.service.mapstruct.RollDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author WYH
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-03-14
 **/
@Service
@RequiredArgsConstructor
public class RollDetailServiceImpl implements RollDetailService {

    private final RollDetailRepository rollDetailRepository;
    private final RollDetailMapper rollDetailMapper;

    @Override
    public Map<String, Object> queryAll(RollDetailQueryCriteria criteria, Pageable pageable) {
        Page<RollDetail> page = rollDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(rollDetailMapper::toDto));
    }

    @Override
    public List<RollDetailDto> queryAll(RollDetailQueryCriteria criteria) {
        return rollDetailMapper.toDto(rollDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public RollDetailDto findById(String id) {
        RollDetail rollDetail = rollDetailRepository.findById(id).orElseGet(RollDetail::new);
        ValidationUtil.isNull(rollDetail.getId(), "RollDetail", "id", id);
        return rollDetailMapper.toDto(rollDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RollDetailDto create(RollDetail resources) {
        resources.setId(IdUtil.simpleUUID());
        return rollDetailMapper.toDto(rollDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RollDetail resources) {
        RollDetail rollDetail = rollDetailRepository.findById(resources.getId()).orElseGet(RollDetail::new);
        ValidationUtil.isNull(rollDetail.getId(), "RollDetail", "id", resources.getId());
        rollDetail.copy(resources);
        rollDetailRepository.save(rollDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            rollDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RollDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RollDetailDto rollDetail : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("主表id", rollDetail.getMainId());
            map.put("物料编号", rollDetail.getElNo());
            map.put("物料名称", rollDetail.getElName());
            map.put("从库区", rollDetail.getFromWaNo());
            map.put("至库区", rollDetail.getToWaNo());
            map.put("数量", rollDetail.getMrQty());
            map.put("批次号", rollDetail.getLotNo());
            map.put("创建时间", rollDetail.getCreateTime());
            map.put("状态", rollDetail.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<RollDetail> getRollDetailsByRollId(String id) {
        List<RollDetail> rollDetailList = rollDetailRepository.getRollDetailsByRollId(id);
        return rollDetailList;
    }
}